package com.smarttable.ysd;

import java.util.ArrayList;

import com.smarttable.ysd.adapter.CategoryDetailGridViewAdapter;
import com.smarttable.ysd.bean.AppInfo;
import com.smarttable.ysd.AppManager;
import com.smarttable.ysd.manager.SharedPreferenceManager;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

public class CategoryDetailActivity extends AppCompatActivity {
  private GridView categoryDetailGrid;
  private CategoryDetailGridViewAdapter categoryDetailGridAdapter;
  private ArrayList<AppInfo> apps;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category_detail);
    AppManager.currentAppCategory = 0;
    if (getIntent().hasExtra("CATEGORY")) {
      showAppIcons(getIntent().getIntExtra("CATEGORY", 0));
    }
    
    categoryDetailGrid = (GridView) findViewById(R.id.category_detail_grid);
    apps = new ArrayList<AppInfo>();
    categoryDetailGridAdapter = new CategoryDetailGridViewAdapter(CategoryDetailActivity.this,apps );
    categoryDetailGrid.setAdapter(categoryDetailGridAdapter);

  }
  @Override
  protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
    if (intent.hasExtra("CATEGORY")) {
      showAppIcons(intent.getIntExtra("CATEGORY", 0));
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
    // TODO 刷新
    Log.d("berrytao", "onResume-------------");
    apps.clear();
    ArrayList<AppInfo> infos = AppManager.getInstance().getAppInfoByCategoryFromXMLFile(AppManager.currentAppCategory, CategoryDetailActivity.this);
    if (infos!=null && infos.size()>0) {
      apps.addAll(infos);
    }
    categoryDetailGridAdapter.notifyDataSetChanged();
    
    categoryDetailGrid.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        ImageView appIcon = (ImageView) view.findViewById(R.id.item_app_icon);
        String type = (String) appIcon.getTag(R.id.category_detail_grid_item_type_id);
        if ("add".equals(type)) {
          if (isUnlock()) {
            Intent intent = new Intent(CategoryDetailActivity.this, AllAppListActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
          } else {
            gotoUnlock();
          }

        } else {
          AppInfo info = (AppInfo) appIcon.getTag(R.id.category_detail_grid_item_icon_id);
          Intent intent = new Intent();
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setComponent(info.getCn());
          startActivity(intent);
        }
      }

    });
    categoryDetailGrid.setOnItemLongClickListener(new OnItemLongClickListener() {

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        ImageView appIcon = (ImageView) view.findViewById(R.id.item_app_icon);
        String type = (String) appIcon.getTag(R.id.category_detail_grid_item_type_id);
        if ("app".equals(type)) {
          if (isUnlock()) {
            AppInfo info = (AppInfo) appIcon.getTag(R.id.category_detail_grid_item_icon_id);
            showDelAppIconDialog(info);
          } else {
            gotoUnlock();
          }
        }
        return true;
      }

    });
  }

  private boolean isUnlock()
  {
    return SharedPreferenceManager.getInstance(this).isGestureUnLock();
  }

  private void gotoUnlock()
  {
    Intent intent = new Intent(CategoryDetailActivity.this, UnlockGesturePasswordActivity.class);
    intent.putExtra("unlock_type","operation_app");
    startActivity(intent);
    overridePendingTransition(R.anim.push_bottom_in, 0);
  }

  private void showAppIcons(int category){
    Log.d("berrytao", "------CategoryDetailActiivty get category = "+category);
    AppManager.currentAppCategory = category;
    switch (category) {
      case 1:
        setTitle(getString(R.string.entertainment_zh));
        break;
      case 2:
        setTitle(getString(R.string.news_zh));
        break;
      case 3:
        setTitle("测试初始状态为空");
        break;
      default:
        break;
    }
  }

  private void showDelAppIconDialog(final AppInfo appInfo){
    Builder builder = new Builder(CategoryDetailActivity.this);
    builder.setMessage("确认删除吗?");
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
     @Override
    public void onClick(DialogInterface dialog, int which) {
       AppManager.getInstance().delAppInfoFromTheirXMLFile(AppManager.currentAppCategory, appInfo, CategoryDetailActivity.this);
       //刷新
       apps.clear();
       ArrayList<AppInfo> infos = AppManager.getInstance().getAppInfoByCategoryFromXMLFile(AppManager.currentAppCategory, CategoryDetailActivity.this);
       if (infos!=null && infos.size()>0) {
         apps.addAll(infos);
       }
       categoryDetailGridAdapter.notifyDataSetChanged();
       dialog.dismiss();
    }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        dialog.dismiss();
      }
    });
    builder.create().show();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    SharedPreferenceManager.getInstance(this).saveGestureUnLock(false);
  }
}
